//
//  BottomSelectTableView.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/15.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "BottomSelectTableView.h"


static NSString *const IDE = @"BottomSelectTableView";

@interface BottomSelectTableView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)NSMutableArray <boModel*>*dataSourceArray;

@property (nonatomic,strong) UIImageView *hideView;

@property (nonatomic,copy) ClickBlock block;

@end

@implementation BottomSelectTableView
- (instancetype)initWithFrame:(CGRect)frame anDataArray:(NSMutableArray <boModel*>*)array ClickBlock:(ClickBlock)block{
    if ([super initWithFrame:frame]) {
        _dataSourceArray = array;
        _block = block;
        self.delegate = self;
        self.dataSource = self;
        self.tableFooterView = [UIView new];
    }
    return self;
}
- (void)HideSelf:(NSInteger)index{
    if (_block) {
        _block(index);
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *templearray = [NSMutableArray new];
    for (int i =0 ; i<self.dataSourceArray.count; i++) {
           boModel *model = self.dataSourceArray[i];
        if (i == indexPath.row) {
            model.ischoosed = @1;
        }else{
            model.ischoosed = @0;
        }
        [templearray addObject:model];
    }
    self.dataSourceArray = templearray;
    
    [tableView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self HideSelf:indexPath.row];
    });

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PayPatternCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"PayPatternCell" owner:nil options:NULL] firstObject];
    boModel *model = self.dataSourceArray[indexPath.row];
    cell.model = model;
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [UIView new];
    headView.backgroundColor = [UIColor qmui_colorWithHexString:@"#F8F8FF"];
    UILabel *infoLb = [[UILabel alloc]initWithFont:[UIFont systemFontOfSize:14] textColor:RGBCOLOR(61, 61, 61)];
    infoLb.text = @"请选择支付方式";
    infoLb.textAlignment = NSTextAlignmentCenter;
    [infoLb sizeToFit];
    infoLb.center = CGPointMake(KWidth/2, 20);
    [headView addSubview:infoLb];
    [headView addSubview:self.hideView];
    self.hideView.frame = CGRectMake(KWidth-30, 10, 20, 20);
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UIImageView *)hideView{
    if (!_hideView) {
        _hideView = [UIImageView new];
        _hideView.userInteractionEnabled = YES;
        _hideView.image = UIImageMake(@"dismissicon");
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(HideSelf)];
        [_hideView addGestureRecognizer:tap];
        //        _hideView.backgroundColor = RGBCOLOR(217, 217, 217);
    }
    return _hideView;
}

@end
